import React, {Component} from 'react';
import {inject, observer} from 'mobx-react';
import {toJS} from 'mobx';
import cssModules from 'react-css-modules';
import classnames from 'classnames';
import {Form, Row, Button, Radio} from 'antd';
import {FormInput, FormItemCustom} from '../../component/FormItem';
import {addUnitInfo} from 'util/api';
import styles from './style.less';

@inject('CardInputOpenStore')
@observer
@cssModules(styles)
class AddUnitContent extends Component {
  constructor(props) {
    super(props);
    this.store = this.props.CardInputOpenStore;
  }

  render() {
    const {fields, handleFormChange} = this.store;

    let scale = Math.ceil((140 / 600) * 24);
    const formItemLayout = {
      labelCol: {
        sm: {span: scale},
      },
      wrapperCol: {
        sm: {span: 24 - scale},
      },
    };
    const CustomRadioComponent = props => {
      const {children = []} = props;

      return (
        <Row className={styles['custom-radio-wrap']}>
          {children.map((item, index) => {
            let isSelected = item.value === props[props.name].value;

            return (
              <Button
                key={index.toString(36)}
                className={classnames(
                  styles['radio-button'],
                  isSelected && styles['radio-selected'],
                )}
                disabled={props.disabled}
                onClick={() => {
                  let obj = {};
                  props[props.name]['value'] = item.value;
                  obj[props.name] = props[props.name];
                  handleFormChange(obj);
                }}>
                {item.name}
              </Button>
            );
          })}
        </Row>
      );
    };
    return (
      <Form
        {...formItemLayout}
        className={styles['add-users-wrap']}
        autoComplete="off">
        <FormInput
          {...fields}
          onChange={handleFormChange}
          label={'编号'}
          disabled={true}
          name={'cardId'}
          rules={[
            {
              required: true,
              message: '必须填写内容!',
            },
          ]}
        />
        <FormInput
          {...fields}
          onChange={handleFormChange}
          label={'卡号'}
          disabled={true}
          name={'cardNumber'}
          rules={[
            {
              required: true,
              message: '必须填写内容!',
            },
          ]}
        />
        <FormInput
          {...fields}
          onChange={handleFormChange}
          label={'所属户室'}
          visiable={fields.cardType.value === 1 ? true : false}
          disabled={true}
          name={'unitId'}
          rules={[
            {
              required: false,
              message: '必须填写内容!',
            },
          ]}
        />
        <FormItemCustom
          {...fields}
          onChange={handleFormChange}
          label={'卡片类型'}
          disabled={true}
          name={'cardType'}
          rules={[
            {
              required: true,
              message: '必须填写内容!',
            },
          ]}
          children={[{value: 1, name: '业主卡'}, {value: 2, name: '工作卡'}]}
          RenderComponet={CustomRadioComponent}
        />
        <FormInput
          {...fields}
          onChange={handleFormChange}
          label={'创建时间'}
          disabled={true}
          name={'createTime'}
          rules={[
            {
              required: false,
              message: '必须填写内容!',
            },
          ]}
        />
        <FormInput
          {...fields}
          onChange={handleFormChange}
          label={'使用者'}
          disabled={false}
          name={'cname'}
          rules={[
            {
              required: false,
              message: '必须填写内容!',
            },
          ]}
        />
      </Form>
    );
  }
}

export default AddUnitContent;
